import {useUserInfoStore} from "../stores/modules/userInfo";
import {ref} from "vue";
import {onLoad, onPageScroll} from "@dcloudio/uni-app";
import {goldRedeemAPI, redeemAPI} from "../services";
import {getConfigAPI} from "../services/me";

export const goldRedeem = () => {
    // 用户信息
    const userInfoStore = useUserInfoStore()
    const redeemList = ref([])
    // 当前tab下标
    const currentTabIndex = ref(0)

    onLoad(async () => {
        await getTaskInfo()
        await getRedeemList(currentTabIndex.value)
    })

    // 返回
    const okBack = () => {
        uni.navigateBack()
    }

    // 点击兑换
    const click = async (item) => {
        currentTabIndex.value = item.index
        await getRedeemList(currentTabIndex.value)
    }

    // 兑换
    const getRedeemList = async (type) => {
        const res = await goldRedeemAPI(type)
        redeemList.value = res.result
    }

    const redeem = async (id) => {
        if (userInfoStore.isLogin()) {
            const res = await redeemAPI(id)
            await getTaskInfo()
            uni.showToast({title: res.msg, icon: 'none', duration: 3000})
        }
    }

    // 我的金币
    const config = ref({
        week_member_gold_coin: 0,
        month_member_gold_coin: 0,
        my_gold_coin: 0
    })
    // 任务配置
    const getTaskInfo = async () => {
        const configRes = await getConfigAPI()
        config.value = configRes.result
    }
    const scrollTop = ref(0)
    onPageScroll(e => {
        console.log("onScrollChange", e.scrollTop)
        scrollTop.value = e.scrollTop
    })
    // 菜单Tab
    const listMenu = ref([
        {name: '全部'},
        {name: '领食会员'},
        {name: '视频会员'},
        {name: '音乐会员'},
        {name: '美食好物'}
    ]);

    return {
        userInfoStore,
        redeemList,
        currentTabIndex,
        config,
        scrollTop,
        listMenu,
        okBack,
        click,
        redeem,
        getTaskInfo,
    }

}